import requests
import json

class RunMethod:
    def post_main(self,url,data,header=None):
        res = None
        if header != None:
            res = requests.post(url=url,data=data,headers=header)
            print(res.status_code)
        else:
            res = requests.post(url=url,data=data)
            print(res.status_code)


        return res.json()

    def get_main(self,url,data=None,header=None):
        res = None
        if header != None:
            res = requests.get(url=url, params=data, headers=header)
            print(res.status_code)
        else:
            res = requests.get(url=url, params=data)
            print(res.status_code)

        return res.json()

    def run_main(self,method,url,data=None,header=None):
        if method == 'post':
            res = self.post_main(url,data,header)
        else:
            res = self.get_main(url,data,header)
        return  json.dumps(res,ensure_ascii=False,sort_keys=True,indent=2)

if __name__ == '__main__':
    data = {
        'username': 'wangyu',
        'password': '123'
    }
    test = RunMethod()
    a = test.get_main(url='http://127.0.0.1:8000/list', data=data)
    print(a)

    b = test.post_main(url='http://127.0.0.1:8000/login/', data=data)
    print(b)